package cmd.help;

import java.io.File;
import java.util.Collection;

import cmd.core.FileManager;

public class CmdExecutor {

	FileManager manager;

	public CmdExecutor(FileManager manager) {
		this.manager = manager;
	}

	public boolean execute(Cmd toExecute, String arg) {
		switch (toExecute) {
		case LS:
			outputCollection(arg == null ? manager.getDirectoryStucture()
					: manager.getDirectoryStucture(arg));
			break;
		case CD:
			manager.changeDirectory(arg);
			break;
		case TOUCH:
			manager.createFile(arg);
			break;
		case MKDIR:
			manager.createDirectory(arg);
			break;
		case HELP:
			helpContent();
			break;
		default:
			return false;
		}
		return true;
	}

	private void helpContent(){
		System.out.printf("%-20s - Output list of Files\n","ls [path]");
		System.out.printf("%-20s - Change directory\n","cd [path] ");
		System.out.printf("%-20s - Create file\n","touch {name}");
		System.out.printf("%-20s - Create directory\n","mkdir {name}");
		System.out.printf("%-20s - Help\n","help");
		System.out.printf("%-20s - Exit\n","exit");
	}
	
	private void outputCollection(Collection<File> toOut) {
		if(toOut == null)
			return;
		for (File i : toOut)
			System.out.printf("%-25s\t%s\n", i.getName(),
					(i.isDirectory() ? "DIR" : "FILE"));
	}
}
